package controller;

import implementor.ChequeDaoImplementor;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

import utilities.Constants;
import utilities.ViewUtilites;
import views.accounting.ChequeReport;

import daoInterface.ChequeDao;
import dbhandeling.Crude;
import dto.Cheque;

public class ChequeReportController extends Crude implements ActionListener
{
	public ChequeReport view;
	public Cheque model;
	Cheque[] cheque;
	
	
	
	public ChequeReportController() {
		// TODO Auto-generated constructor stub
	}

	

	public ChequeReportController(ChequeReport view, Cheque model) {
		super();
		this.view = view;
		this.model = model;
		view.actionListner(this);
		try {
			this.setTableOnGui();
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	

	public ChequeReport getView() {
		return view;
	}



	public void setView(ChequeReport view) {
		this.view = view;
	}



	public Cheque getModel() {
		return model;
	}



	public void setModel(Cheque model) {
		this.model = model;
	}

	public void setTableOnGui() throws SQLException, Exception
	{
		ChequeDao chequeImp=new ChequeDaoImplementor();
		cheque=chequeImp.retrieveArray("status", 1+"");
		
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		for(int i=0;i<cheque.length;i++)
		{
			dm.addRow(new Object[]{1,25/11/2013,cheque[i].getamt(),"Comments",cheque[i].getChequeNo(),cheque[i].getdate(),cheque[i].getbankName(),cheque[i].getbranchName()});
		}
		
		
		
	}
	
	
	
	public void reactOnUpdate() throws SQLException, Exception
	{
		ChequeDao chequeImp=new ChequeDaoImplementor();
		DefaultTableModel dm=(DefaultTableModel) view.getTable().getModel();
		int columncount=dm.getColumnCount();
		int rowcount=dm.getRowCount();
		
		for(int i=0;i<rowcount;i++)
		{
			System.out.println(i);
			if((Boolean) dm.getValueAt(i,8))
			{
				Cheque ch=cheque[i];
				ch.setstatus(0);
				chequeImp.update(ch);
				
				
			}
		}
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		String k=e.getActionCommand();
		
		if(k.contains("Update"))
		{
			try {
				reactOnUpdate();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

}
